<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.model.Resource"%>
<%@ page import="com.control.ResourceControl"%>
<%@ page import="com.util.PageResultSet"%>
<%@ page import="com.control.DormControl" %>
<%@ page import="com.model.Dorm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>查看物品</title>
	</head>
	<body>
		<%
		    request.setCharacterEncoding("UTF-8");
		    String ToolBar = "";
		    int curPage = 1;
	    	ArrayList<Resource> list = new ArrayList<Resource>();
	    	ResourceControl rc = new ResourceControl();
	    	String res_name = "";
	    	if(request.getParameter("res_name") != null){
	    		res_name = request.getParameter("res_name");
	    		session.setAttribute("res_name",res_name);
	    	}
	    	list = rc.getResInformation(res_name);
	    	if(request.getParameter("cur_page")!= null) {
				curPage = Integer.parseInt(request.getParameter("cur_page")); 
			}
			PageResultSet p = new PageResultSet(list,curPage);
			ArrayList<Resource> nowlist = new ArrayList<Resource>();
			nowlist = (ArrayList<Resource>)p.getData();
		%>
		<table width="98%" border="0" align="center">
			<tr>
				<td></td>
			</tr>
			<tr>
				<td></td>
			</tr>
			<tr>
				<td>
					<fieldset>
						<legend>
							物品列表
						</legend>
						<form action="resource_show.jsp" name="form1" method="post">
							<table width="100%" border="0" cellspacing="0" cellpadding="0">
								<tr height="10">
									<td></td>
								</tr>
								<tr align="center">
									<td align="center">
								    <%
								       	if(session.getAttribute("res_name") != null) {
	    									res_name = (String)session.getAttribute("res_name");
	    								}
							        %>
								       	物品名
								        <input name="res_name" type="text" id="dorm" size="5" value="<%=res_name %>"/>
										<input type="submit" value="查询" />
										<input name="insert" type="button" value="添加物品"
											onclick="window.location.href='add_resource.jsp'" />
									</td>
								</tr>
							</table>
							<table width="70%" border="0" align="center" cellspacing="0"
								cellpadding="0">
								<tr>
									<td height="20"></td>
								</tr>
								<tr>
									<td>
										<table border="1" width="99%" align="center" cellpadding="0"
											cellspacing="1" class="resultTable">
											<tr>
												<td width="4%" height="25" align="center" bgcolor="#D9F0FF">
													物品名称
												</td>
												<td width="2%" height="25" align="center" bgcolor="#D9F0FF">
													数量
												</td>
												<td width="5%" height="25" align="center" bgcolor="#D9F0FF">
													描述
												</td>
												<td width="4%" height="25" align="center" bgcolor="#D9F0FF">
													操作
												</td>
								</tr>
								<%
										
										
										for(int i=0; i<nowlist.size(); i++) {
											Resource resource = nowlist.get(i);
									 %>
								 	<tr>
        								<td height="25" align="center"><%=resource.getRes_name()%></td>
 										<td height="25" align="center"><%=resource.getRes_num()%></td>
 										<td height="25" align="center"><%=resource.getRes_des()%></td>
 		 								<td height="25" align="center"><a href="update_resource.jsp?id=<%=resource.getRes_id()%>">修改</a> 
 		 								| <a href="resource_show.jsp?id=<%=resource.getRes_id()%>">派发</a>
 		 								| <a href="javascript:if(confirm('确认删除吗?'))window.location='delete_resource.jsp?id=<%=resource.getRes_id()%>'">删除</a></td>
 									</tr>
   									<%}
   										
										String fileName = "resource_show.jsp";
										ToolBar = p.getToolBar(fileName);
										//System.out.println(ToolBar);
									%>
							</table>
							<%=ToolBar %>
							
							</td>
							</tr>
							</table>
						</form>
					</fieldset>
				</td>
			</tr>
		</table>
		<% 
			if(request.getParameter("id")!=null){
				int res_id = Integer.parseInt(request.getParameter("id"));
				int admin_id = (Integer)session.getAttribute("user_id");
				DormControl dc = new DormControl();
				ArrayList<Dorm> dormlist = dc.getDorm(admin_id);
				int dorm_num = dormlist.size();
				Resource res = rc.getResource(res_id);
				int res_num = res.getRes_num();
				int divide = res_num / dorm_num;
				if(divide == 0){
		%>
				<script>
					alert("物品数量不够分配");
					window.location.href="resource_show.jsp";
				</script>
		<%
				}
				else{
		%>
				<script type="text/javascript">
    				var num=prompt("请输入数据：1-<%=divide%>","");	
    		 		if(num!="" && num!=null)
						location="distribute_resource.jsp?dorm_num=<%=dorm_num%>&divide=<%=divide%>&res_id=<%=res_id%>&res_num=<%=res_num%>&dorm_num=<%=dorm_num%>&num="+num;
 				</script>
 		<%}}%>
	</body>
</html>
